<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('checkboxes');

class JFormFieldListXmlAttributes extends JFormFieldCheckboxes
{

    public $type = 'ListXmlAttributes';

    protected function getOptions()
    {
        $aOptions = array();

        $oConfig = JFactory::getConfig();

        // Get the tables of component.
        $oApplication = JFactory::getApplication();
        
		$iXmlTypeId = $oApplication->input->getInt('iXmlTypeId');
        
        // Set the XML attributes array base on layout.
		$sLayout = $oApplication->input->getString('layout', 'add');
		
		switch($sLayout)
		{
			case 'add':
				$aXmlAttributes = $this->getAttributesToAdd($iXmlTypeId);
				break;
				
			case 'delete':
				$aXmlAttributes = $this->getAttributesToDelete($iXmlTypeId);
				break;
		}
		
		foreach ($aXmlAttributes as $oAttribute)
        {
			$oOption = new stdClass;
            
			$oOption->text = $oAttribute->title . '="' . $oAttribute->default . '"';
			$oOption->value = $oAttribute->id;
            $oOption->default = $oAttribute->default;

			$aOptions[] = $oOption;
		}

        return $aOptions;
    }

	protected function getAttributesToAdd($iXmlTypeId)
	{
		$oDatabase = JFactory::getDbo();
        
		$oQuery = $oDatabase->getQuery(true);
	
		$oQuery->select('axa.*');
		
		$oQuery->from('#__auto_xml_attribute AS axa');
		
		$oQuery->where('axa.id NOT IN ( 
            SELECT axe.xml_attribute_id 
            FROM #__auto_xml_element AS axe 
            WHERE axe.xml_type_id = ' . $iXmlTypeId . ')');
		
		$oQuery->ordering('axa.title ASC');
        
        $oDatabase->setQuery($oQuery);
        
        try
        {
            $aXmlAttributes = $oDatabase->loadObjectList();
        }
        catch (RuntimeException $oException)
        {
            JError::raiseWarning(500, $oException->getMessage());
        }
		
		return $aXmlAttributes;
	}
	
	protected function getAttributesToDelete($iXmlTypeId)
	{
		$oDatabase = JFactory::getDbo();
        
		$oQuery = $oDatabase->getQuery(true);
	
		$oQuery->select('axe.*, axa.title, axa.default, axa.description');
				
		$oQuery->from('#__auto_xml_element AS axe');
		
		$oQuery->join('left', '#__auto_xml_attribute AS axa ON axe.xml_attribute_id = axa.id');
		
		$oQuery->where('axe.xml_type_id = ' . $iXmlTypeId);
		
		$oQuery->ordering('axa.title ASC');
		
		$oDatabase->setQuery($oQuery);

        try
        {
            $aXmlAttributes = $oDatabase->loadObjectList();
        }
        catch (RuntimeException $oException)
        {
            JError::raiseWarning(500, $oException->getMessage());
        }
		
		return $aXmlAttributes;
	}
	
    protected function getInput()
    {
        $aHtml = array();

        $sClass = $this->element['class'] ? ' class="checkboxes ' . (string) $this->element['class'] . '"' : ' class="checkboxes"';

        $aCheckedOptions = explode(',', (string) $this->element['checked']);

        $aHtml[] = '<span class="btn check-all" onclick="Autogenerator.XML_Element.checkAll();">' . JText::_('COM_AUTOGENERATOR_IMPORT_FIELD_TABLES_CHECKBOX_TOGGLE_LABEL') . '</span>';
        $aHtml[] = '&nbsp;';
        $aHtml[] = '<span class="btn check-all" onclick="Autogenerator.XML_Element.clearAll();">' . JText::_('COM_AUTOGENERATOR_IMPORT_FIELD_TABLES_CHECKBOX_CLEAR_LABEL') . '</span>';
        
        // Start the checkbox field output.
        $aHtml[] = '<fieldset id="' . $this->id . '"' . $sClass . '>';

        // Get the field options.
        $aOptions = $this->getOptions();
        
        
        
        // Build the checkbox field output.
        $aHtml[] = '<ul>';

        foreach ($aOptions as $iKey => $oOption)
        {
            // Initialize some option attributes.
            if (!isset($this->value) || empty($this->value))
            {
                $sChecked = (in_array((string) $oOption->value, (array) $aCheckedOptions) ? ' checked="checked"' : '');
            }
            else
            {
                $sValue = !is_array($this->value) ? explode(',', $this->value) : $this->value;

                $sChecked = (in_array((string) $oOption->value, $sValue) ? ' checked="checked"' : '');
            }

            $sClass = !empty($oOption->class) ? ' class="' . $oOption->class . '"' : '';

            $sDisabled = !empty($oOption->disable) ? ' disabled="disabled"' : '';

            // Initialize some JavaScript option attributes.
            $sOnclick = !empty($oOption->onclick) ? ' onclick="' . $oOption->onclick . '"' : '';

            $aHtml[] = '<li>';
            
            $aHtml[] = '<input type="checkbox" id="' . $this->id . $iKey . '" name="' . $this->name . '"' . ' value="' . intval($oOption->value) . '"' . $sChecked . $sClass . $sOnclick . $sDisabled . '/>';
            
            $aHtml[] = '<input type="hidden" name="aDefault[' . intval($oOption->value) . ']"' . ' value="' . htmlspecialchars($oOption->default, ENT_COMPAT, 'UTF-8') . '"/>';
            
            $aHtml[] = '<label for="' . $this->id . $iKey . '"' . $sClass . '>' . JText::_($oOption->text) . '</label>';
            
            $aHtml[] = '</li>';
        }

        $aHtml[] = '</ul>';

        // End the checkbox field output.
        $aHtml[] = '</fieldset>';

        return implode($aHtml);
    }

}
